package myProj2;

import java.util.ArrayList;
import java.util.Scanner;

import myProj2.Code;
import myProj2.Project2;

public class Provided {
	/**
	 * used to make a random solution
	 * @return a code chosen with no restriction on the four choices
	 */
	public static Code randColors(){ //completely random selection
		Project2.Color[] secSlots = new Project2.Color[4];
		for(int i = 0; i<4; i++){
			secSlots[i] = Project2.Color.values()[Project2.rangen.nextInt(7)];
		}
		return new Code(secSlots);
	}
	/**
	 * used when testing different AI starting guesses
	 * @return a Code with every color different, in a random order
	 */
	public static Code allDiff(){
		Project2.Color[] answer = new Project2.Color[4];
		ArrayList<Project2.Color> opts = new ArrayList<Project2.Color>();
		for(int i=0;i<7;i++){
			opts.add(Project2.Color.values()[i]);
		}
		for(int i=0;i<4;i++){
			answer[i] = opts.remove(Project2.rangen.nextInt(7-i));
		}
		return new Code(answer);
	}
	/**
     * used when testing different AI starting guesses
	 * @return a Code with every color the same
	 */
	public static Code allSame(){
		Project2.Color[] answer = new Project2.Color[4];
		answer[0]=Project2.Color.values()[Project2.rangen.nextInt(7)];
		for(int i=1;i<4;i++){
			answer[i]=answer[0];
		}
		return new Code(answer);
	}

	/**
	 * 
	 * @return a random code of two double colors
	 */
	public static Code twoDubs(){  // 
		Project2.Color[] secret = new Project2.Color[4];
		int pair1, pair2;
		Project2.Color col1, col2;
		pair1 = Project2.rangen.nextInt(4);
		do{ //in general, this is not a good way to do this, since it might loop many times
			pair2 = Project2.rangen.nextInt(4);
		} while(pair2==pair1);
		col1 = Project2.Color.values()[Project2.rangen.nextInt(7)];
		do{
			col2 = Project2.Color.values()[Project2.rangen.nextInt(7)];
		} while(col1==col2);
		for(int i=0; i<4; i++){
			if(i==pair1 || i==pair2){
				secret[i]=col1;
			}else{
				secret[i]=col2;
			}
		}
		return new Code(secret);
	}


	/**
	 * does input validation for exactly 4 entries
	 * @return gives array of Colors (based on first letter)
	 */
	public static Code inputColors(){
		String options = "EROYGBV";
		boolean goodAns;
		String entry, letter;
		int pos, numWords;
		Project2.Color[] result = new Project2.Color[4];
		do{
			do{	
				goodAns=true;
				numWords = 0;
				System.out.print("CodeBreaker, enter exactly four colors: ");
				entry = Project2.scnr.nextLine();
				if (entry.length()==0){
					System.out.println("You must make an entry, try again.");
				}
			} while (entry.length()==0);
			Project2.entryScnr = new Scanner(entry);
			while(goodAns && Project2.entryScnr.hasNext()){
				letter = Project2.entryScnr.next().toUpperCase().substring(0,1);
				pos = options.indexOf(letter);
				goodAns = goodAns&&pos!=-1&&numWords<4;
				if(goodAns){
					result[numWords++]=Project2.Color.values()[pos];
					if(numWords==4 && Project2.entryScnr.hasNext()){
						goodAns = false;
					}
				}
			}
			if(!(goodAns=goodAns&&numWords==4)){// adjusting to invalid if not 4 words
				System.out.println("You must enter exactly four colors, try again.");
			}
		} while(!goodAns);
		return new Code(result);
	}
	/**
	 * does input validation on the num of black&white pegs
	 * insuring 0<=sum<=4 and each individual between 0 to 4.
	 * @return  mod 10 gives the number of whites, divide by 10 to get num of blacks
	 */
	public static int inputPegs(){
		String entry="";
		int ans = -1, ans2 = -1;
		boolean inRange;
		do{
			do{
				do{
					inRange = true;
					System.out.print("CodeMaker, enter number of black (exact match) pegs: ");
					entry = Project2.scnr.nextLine();			
				}while(entry.length()==0);
				Project2.entryScnr = new Scanner(entry);
			}while(!Project2.entryScnr.hasNextInt());
			ans = Project2.entryScnr.nextInt();
			inRange = -1 < ans && ans < 5;
			if(!inRange) System.out.println("must enter a value between 0 and 4");
		}while(!inRange);
		do{
			do{
				do{
					inRange = true;
					System.out.print("CodeMaker, enter number of White (wrong position) pegs: ");
					entry = Project2.scnr.nextLine();			
				}while(entry.length()==0);
				Project2.entryScnr = new Scanner(entry);
			}while(!Project2.entryScnr.hasNextInt());
			ans2 = Project2.entryScnr.nextInt();
			inRange =  -1 < ans2 && ans2 < 5 && (ans+ans2)<5;
			if(!inRange) System.out.println("white between 0 and 4, and 0<=black+white<=4 too");
		}while(!inRange);
		return ans*10+ans2;
	}
}
